import URIJS from 'urijs'
import request from "@/utils/request";
import * as storage from "@/utils/storageUtils";
window.saveSetting = function (host, clientNum) {
  storage.save("host", host);
  storage.save("clientNum", clientNum);
};
window.getSetting = function () {
  const host = storage.get("host");
  const clientNum = storage.get("clientNum");
  return { host, clientNum };
};
function doPingjia(url) {
  console.log(url);
  const reqUrl = storage.get("host") + "/api/device/push";
  console.log(URIJS.parseQuery(URIJS.parse(url).query))
  const params = { clientNum: storage.get("clientNum"),...URIJS.parseQuery(URIJS.parse(url).query)};
  request.post(reqUrl, params);
}
chrome.webRequest.onBeforeRequest.addListener(
  (details) => {
    if (details.url.indexOf("http://localhost:3002/app/evaluate") != -1) {
      doPingjia(details.url);
      return { cancel: true };
    } else {
      return { cancel: false };
    }
  },
  { urls: ["<all_urls>"], types: ["xmlhttprequest"] },
  ["blocking"]
);
